package com.yuyou.tas.lesson.controller;

import com.yuyou.tas.lesson.service.IBaseTeaService;
import com.yuyou.tas.service.api.lesson.dto.DtoBaseTeach;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.PageParams;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="BaseTeaController",description="教材绑定接口")
@Validated
@RestController
@RequestMapping(value="/api/BaseTea")
public class BaseTeaController extends BaseController {

    @Autowired
    private IBaseTeaService service;

    /*@Autowired
    private OrganLimitService organLimitService;


    @ApiOperation(value="绑定教材")
    @RequestMapping(value="/addTeach",method = RequestMethod.POST)
    public ReturnResult addTeach(@Validated @ApiParam(value = "科目ID")@NotNull(message = "科目不能为空")@RequestParam(value = "subjectId") Long SubjectId,
                                 @Validated @ApiParam(value = "教材ID")@NotNull(message = "教材ID不能为空")@RequestParam(value = "materialId") Long materialId,
                                 @Validated @ApiParam(value = "年级ids")@NotEmpty(message = "年级ids不能为空")@RequestParam("sectionIds") String[] gradeIds,
                                 @Validated @ApiParam(value = "类型：0：普通，1：全国")@RequestParam(value = "type",required = false) Integer type,
                                  HttpServletRequest request){
        LoginRedisObj user = this.getUserObj(request);
        ResBoolSimpleInfo info = service.addTeach(SubjectId ,materialId , String.join(",",gradeIds) , type , user,user.getOrganizationId());
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    @ApiOperation(value="删除教材绑定")
    @RequestMapping(value="/delTeach",method = RequestMethod.POST)
    public ReturnResult delTeach(@Validated @ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id") Long id,
                                 HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_teach);
        }
        ResBoolSimpleInfo info = service.delTeach(id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }

    @ApiOperation(value="通过ID获取教材绑定信息")
    @RequestMapping(value="/getBaseTeach",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<BaseTeach> getBaseTeach(@Validated @NotNull(message = "ID不能为空")@ApiParam(value = "ID" , required = true) @RequestParam(value = "id") Long id,
                                                HttpServletRequest request) throws Exception {
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_teach);
        }
        ResBoolSimpleInfo info =  service.getTeachByID(id);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , info.getData());
    }


    @ApiOperation(value="修改教材绑定信息")
    @RequestMapping(value="/updateTeach",method = RequestMethod.POST)
    public ReturnResult updateTeach( @Validated @ApiParam(value = "ID")@NotNull(message = "ID不能为空")@RequestParam(value = "id") Long id,
                                    @ApiParam(value = "教材ID")@RequestParam(value = "materialId") Long materialId,
                                    @ApiParam(value = "年级ids")@RequestParam("sectionIds") String[] sectionIds,
                                    @ApiParam(value = "科目ID")@RequestParam(value = "subjectId") Long subjectId,
                                    @ApiParam(value = "类型：0：普通，1：全国")@RequestParam(value = "type",required = false) Integer type,
                                     HttpServletRequest request) throws Exception {
        if(materialId == null && subjectId == null && sectionIds == null && sectionIds.length == 1 && type == null){
            throw new BusinessException("没有传递需要修改的参数");
//            return ReturnResult.fail("没有传递需要修改的参数");
        }
        String gradeIds = null;
        if(sectionIds != null){
            gradeIds = String.join(",",sectionIds);
        }
        LoginRedisObj user = this.getUserObj(request);
        if(!user.getIsSystem()) {
            organLimitService.checkOrganByOrganId(user.getOrganizationId(), id, OrganLimitEnums.type.base_teach);
        }
        ResBoolSimpleInfo info = service.updateTeach(materialId , gradeIds ,type ,subjectId ,id,user);
        return ReturnResult.returnRes(info.isSuccess() , info.getMessage() , null);
    }*/

    @ApiOperation(value = "查询教材绑定信息")
    @RequestMapping(value = "/findBaseTeachPage" ,method = {RequestMethod.GET,RequestMethod.POST})
    public ReturnResult<DtoBaseTeach> findBaseTeachPage(HttpServletRequest request,
                                                        @Validated @ModelAttribute PageParams pageParams,
                                                        @ApiParam(value = "数据类型：0标准，1机构")@RequestParam(value = "type",required = false) Integer type,
                                                        @ApiParam(value = "教材ID")@RequestParam(value = "materialId",required = false) String materialId,
                                                        @ApiParam(value = "科目ID")@RequestParam(value = "subjectId",required = false) Long subjectId,
                                                        @ApiParam(value = "年级id")@RequestParam(value = "sectionId",required = false) String gradeId){
        Map<String, Object> conditions = new HashMap<>();
        Pagination<DtoBaseTeach> queryPgn = new Pagination<>();
        LoginRedisObj user = this.getUserObj(request);

        if(!StringUtils.isEmpty(materialId)){
            conditions.put("materialId", materialId.trim());
        }
        if(!StringUtils.isEmpty(gradeId)){
            conditions.put("gradeId", gradeId.trim());
        }

        if(!StringUtils.isEmpty(subjectId)){
            conditions.put("subjectId", subjectId+"");
        }

        if(type != null && type == 0){
            conditions.put("isStan", 1);//获取标准数据
        }else{
            conditions.put("organ_id", user.getOrganizationId());//默认查询个人所在机构的校区
            if(!user.getIsManager() && StringUtils.isEmpty(gradeId)){
                conditions.put("sectionIDs", user.getBaseSectionIds());//获取用户年级权限
            }
            if(!user.getIsManager() && StringUtils.isEmpty(subjectId)){
                conditions.put("subjectIDs", user.getDataSubjects());//获取用户科目权限
            }
        }

        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);

        int total = service.findTeachCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<DtoBaseTeach> list = service.findTeachPageList(queryPgn);
            queryPgn.setList(list);
        }
        return ReturnResult.success(queryPgn);
    }
		
}
